﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace SimpleEntry
{
	// Text Box
	public class TextEditControl : IEditControl
	{
		public TextEditControl(TextBox tb) { m_TextBox = tb; }

		public String Value
		{
			get { return this.m_TextBox.Text; }
			set { this.m_TextBox.Text = value; }
		}

		public Control FormControl
		{
			get { return this.m_TextBox; }
			set { this.m_TextBox = value as TextBox; }
		}

		TextBox m_TextBox;
	}

	// Label
	public class LabelControl : IEditControl
	{
		public LabelControl(Label lbl) { m_Lbl = lbl; }

		public String Value
		{
			get { return this.m_Lbl.Text; }
			set { this.m_Lbl.Text = value; }
		}

		public Control FormControl
		{
			get { return this.m_Lbl; }
			set { this.m_Lbl = value as Label; }
		}

		Label m_Lbl;
	}


	// Combo box
	public class ComboEditControl : IEditControl
	{
		public ComboEditControl(ComboBox cb) { m_ComboBox = cb; }

		public String Value
		{
			get { return this.Selected(); }
			set { SetSelection(value); }
		}

		public Control FormControl
		{
			get { return this.m_ComboBox; }
			set { this.m_ComboBox = value as ComboBox; }
		}

		private void SetSelection(string value)
		{
			for (int idx = 0; idx < m_ComboBox.Items.Count; ++idx)
			{
				String sel = m_ComboBox.Items[idx].ToString();
				if (sel == value)
				{
					this.m_ComboBox.SelectedIndex = idx;
					return;
				}
			}
			if (this.m_ComboBox.DropDownStyle != ComboBoxStyle.DropDownList)
				this.m_ComboBox.Text = value;
		}

		private string Selected()
		{
			if (this.m_ComboBox.SelectedIndex == -1)
			{
				if (this.m_ComboBox.DropDownStyle != ComboBoxStyle.DropDownList)
					return m_ComboBox.Text;
				return "";
			}
			String selected = m_ComboBox.Items[this.m_ComboBox.SelectedIndex].ToString();
			return selected;
		}

		ComboBox m_ComboBox;
	}
	// List box
	public class ListEditControl : IEditControl
	{
		public ListEditControl(ListBox cb) { m_ListBox = cb; }

		public String Value
		{
			get { return this.Selected(); }
			set { SetSelection(value); }
		}

		public Control FormControl
		{
			get { return this.m_ListBox; }
			set { this.m_ListBox = value as ListBox; }
		}

		private void SetSelection(string value)
		{
			for (int idx = 0; idx < m_ListBox.Items.Count; ++idx)
			{
				String sel = m_ListBox.Items[idx].ToString();
				if (sel == value)
				{
					this.m_ListBox.SelectedIndex = idx;
					return;
				}
			}
		}

		private string Selected()
		{
			if (this.m_ListBox.SelectedIndex == -1)
				return "";
			String selected = m_ListBox.Items[this.m_ListBox.SelectedIndex].ToString();
			return selected;
		}

		ListBox m_ListBox;
	}

	// CheckBox
	public class CheckControl : IEditControl
	{
		public CheckControl(CheckBox chk)
		{
			this.m_Chk = chk;
			// below assumes eventual data is a bit or number field
			TrueText = "1";
			FalseText = "0";
		}

		// extended. Define values returned when the checkbox is checked or unchecked.
		public CheckControl(CheckBox chk, String trueText, String falseText)
		{
			this.m_Chk = chk;
			TrueText = trueText;
			FalseText = falseText;
		}

		public String Value
		{
			// 
			get { return (this.m_Chk.Checked) ? TrueText : FalseText; }

			// allow custom,1/0 Y/N Yes/No T/F True/False
			// note unless the inputs match the Truetext/FalseText then the Bindings Dirty check may return false positives.
			set
			{
				String checkVal = value.Trim().ToUpper();
				this.m_Chk.Checked = (checkVal == TrueText.Trim().ToUpper()) ? true :  // override check-it
									 (checkVal == FalseText.Trim().ToUpper()) ? false : // override don't check-it
									 (checkVal.Length == 0) ? false :					// empty should be uncheck
									 (checkVal[0] == 'T' || checkVal[0] == 'Y' || checkVal[0] == '1') ? true :
									 false; // any other input is ignored. Isn't the above enough!
			}
		}

		public Control FormControl
		{
			get { return this.m_Chk; }
			set { this.m_Chk = value as CheckBox; }
		}
		public String TrueText { get; set; }
		public String FalseText { get; set; }
		// single line helper as I best its most often configured as a pair.
		public void SetTrueFalseText(String trueText, String falseText) { TrueText = trueText; FalseText = falseText; }

		CheckBox m_Chk;
	}

	public class RadioGroup : IEditControl
	{
		// simple. returns "0","1".. etc depending on radio selected
		public RadioGroup(RadioButton[] radioControls)
		{
			this.m_Controls = radioControls;
			this.m_Values = new String[this.m_Controls.GetLength(0)];
			for (int idx = 0; idx < m_Values.GetLength(0); ++idx)
				this.m_Values[idx] = idx.ToString();
		}

		// custom array of values associated to the control list
		public RadioGroup(RadioButton[] radioControls, String[] values)
		{
			this.m_Controls = radioControls;
			this.m_Values = values;
		}

		public String Value
		{
			// 
			get
			{
				for (int idx = 0; idx < m_Controls.GetLength(0); ++idx)
				{
					if (this.m_Controls[idx].Checked) return m_Values[idx];
				}
				return "";
			}
			set
			{
				String testVal = value.Trim().ToUpper();
				for (int idx = 0; idx < m_Controls.GetLength(0); ++idx)
				{
					bool bCheck = (this.m_Values[idx].Trim().ToUpper() == testVal) ? true : false;
					this.m_Controls[idx].Checked = bCheck;
				}
			}
		}

		// Not brilliant
		public Control FormControl
		{
			get { return this.m_Controls[0]; }
			set { m_Controls[0] = value as RadioButton; }
		}

		RadioButton[] m_Controls;
		String[] m_Values;
	}
}
